#include "fenetre.h"
#include "ui_fenetre.h"
#include "qmessagebox.h"
#include "qfiledialog.h"


#include "opencv/cv.h"
#include "opencv/highgui.h"



Fenetre::Fenetre(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Fenetre)
{
    ui->setupUi(this);

    QObject::connect(ui->okB, SIGNAL(clicked()), this, SLOT(CheckSplitMerge()));
    QObject::connect(ui->saveB, SIGNAL(clicked()), this, SLOT(SaveResult()));
    QObject::connect(ui->imageKUR, SIGNAL(textChanged(QString)), this, SLOT(AfficheImage()));
    //QObject::connect(ui->seulSplitHS, SIGNAL(sliderMoved(int)), this, SLOT(RefreshSeuilSplit()));
    //QObject::connect(ui->seuilMergeHS, SIGNAL(sliderMoved(int)), this, SLOT(RefreshSeuilMerge()));
}

Fenetre::~Fenetre()
{
    delete ui;
}

void Fenetre::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void Fenetre::CheckSplitMerge() {

    QString imageurl = ui->imageKUR->text();
    QString s = QString::number(ui->seulSplitHS->value());
    QString m = QString::number(ui->seuilMergeHS->value());
    QString colorS;
    bool color;
    if (ui->couleurRB->isChecked())
    {
        colorS = "couleur";
        color = true;
    }
    else
    {
        colorS = "noir et blanc";
        color = false;
    }

    if (imageurl == "")
    {
        QMessageBox msgBox;
        msgBox.setText("Veuillez choisir une image");
        msgBox.setWindowTitle("Attention");
        msgBox.exec();
    }
    else
    {
        QMessageBox msgBox;
        msgBox.setText("split" + s + " merge " + m + " image " + imageurl + " couleur " + colorS);
        msgBox.setWindowTitle("on split and merge");
        msgBox.exec();
        sp = new SplitMerge::SplitMerge(ui->seulSplitHS->value(), ui->seuilMergeHS->value(), imageurl, color);
    }

}



void Fenetre::AfficheImage() {

    QPixmap *pixmap_img = new QPixmap(ui->imageKUR->text());
    pixmap_img->scaled(21,91,Qt::KeepAspectRatio, Qt::FastTransformation);
    ui->imageL->setAlignment(Qt::AlignCenter);
    //ui->imageL->setFixedHeight(pixmap_img->height());
    //ui->imageL->setFixedWidth(pixmap_img->width());
    ui->imageL->setPixmap(*pixmap_img);

}

void Fenetre::SaveResult() {


    QString filtre;
    QString fichier = QFileDialog::getSaveFileName(this, "Enregistrer un fichier", QString(), "Images(*.bmp, *.jpg, *.pmg", &filtre);

   // std::cout<<"le fichier "<< fichier << std::endl;
    //cvSaveImage(fichier,sp->img);
    //QImage img_save = QImage();
    //QPainter painter(&img_save);
    //viewNaff->render(&painter);
    //img_save.save(fichier);
}

/*
void Fenetre::RefreshSeuilSplit() {
    QString m = QString::number(ui->seulSplitHS->value());
    ui->seuilSplitL->setText(m);
}

void Fenetre::RefreshSeuilMerge() {
    QString m = QString::number(ui->seuilMergeHS->value());
    ui->seuilMergeL->setText(m);
}
*/
